package za.org.jcicada.syshealth;

import java.util.Observable;
import java.util.Observer;

/**
 * Base class for controllers. Different variations of the controller could be
 * derived from this although it is debatable how much value this would add.
 * 
 * @author Robert Crida
 */
public abstract class Controller implements Observer {

	/** MVC Model handle */
	private Model model;

	/** MVC View handle */
	private View view;

	/**
	 * Construct a controller with the specified view
	 * 
	 * @param view
	 *            the matching view for this controller
	 */
	public Controller(View view) {
		this.view = view;
		model = view.getModel();
		model.addObserver(this);
	}

	public Model getModel() {
		return model;
	}

	public View getView() {
		return view;
	}

	/**
	 * Default implementation of update method for events from model which does
	 * nothing.
	 */
	public void update(Observable o, Object arg) {
		// default = no op
	}

	/**
	 * Detach from the subject before GC
	 */
	@Override
	protected void finalize() throws Throwable {
		model.deleteObserver(this);
	}

}
